package mytest

import "fmt"

func oprArray(a [10]int) {
	for i := 0; i < len(a); i++ {
		fmt.Printf("%d\n", a[i])
	}
}

func modifyArray(a *[10]int) {
	for i := 0; i < len(a); i++ {
		a[i] = a[i] * 2
	}
}

func modifyArrayUseSlice(a []int) {
	for i := 0; i < len(a); i++ {
		a[i] = a[i] * 3
	}
}

func PrintArray() {
	a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	oprArray(a)
}

func RewriteArray() {
	a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	modifyArray(&a)
	fmt.Println("修改后的数组:")
	oprArray(a)
}

func modifyArrayAlice(a []int) {
	for i := 0; i < len(a); i++ {
		if i == 0 {
			a[i] = 0
		} else {
			a[i] = a[i-1] + 1
		}
	}
}
func RewriteArrayUseSlice() {
	a := [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	aSlice := a[:]
	modifyArrayUseSlice(aSlice)
	fmt.Println("修改后的数组:")
	oprArray(a)
	aSlice = append(aSlice, 11, 12, 13)

	mySlice := make([]int, 10)
	modifyArrayAlice(mySlice)
	oprArray([10]int(mySlice))
	fmt.Println("++++++++++++++++++++++++++++++++++++")
	for i, v := range mySlice {
		fmt.Println("mySlice[", i, "] = ", v)
	}
	mySlice = append(mySlice, 11, 12, 13)
	for i, v := range mySlice {
		fmt.Println("mySlice[", i, "] = ", v)
	}
}

func CopyArraySlice() {
	slice1 := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
	slice2 := []int{10, 20, 30}
	copy(slice2, slice1)
	fmt.Println("----------------------------------------")
	for i, v := range slice2 {
		fmt.Println("slice2[", i, "] = ", v)
	}
	slice2 = []int{10, 20, 30}
	copy(slice1, slice2)
	fmt.Println("----------------------------------------")
	for i, v := range slice1 {
		fmt.Println("slice1[", i, "] = ", v)
	}
}
